package client;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JComponent;

import common.Level;
import common.Player;

public class Canvas extends JComponent implements KeyListener{

	public static final int FIELDSIZE = 40;
	public static final int OFFSET = 20;
	public static final int WALLOFFSET = 3;
	
	private static final long serialVersionUID = 1L;
	
	private Image mImage; // used for double buffering
	private Level level;
	private Player[] player;
	private int playfieldWidth;
	private int playfieldHeight;
	
	public Canvas( Level level, Player[] player){
		this.player = player;
		this.level = level;
		playfieldWidth = FIELDSIZE*level.getRowAmount();
		playfieldHeight = FIELDSIZE*level.getColumnAmount();
		addKeyListener( this);
	}

	/*
	 * zeichnet spielfeld, spieler und punkte
	 */
	public void draw( Graphics g){
		drawLevel(g);
		for( Player p : player){
			p.draw(g);
		}
	}

	/*
	 * keylisterner
	 */
	@Override
	public void keyPressed(KeyEvent ke) {
	  switch( ke.getKeyCode() )
	  {
		  case KeyEvent.VK_DOWN: player[0].moveDown(level);  break;
		  case KeyEvent.VK_UP: player[0].moveUp(level); break;
		  case KeyEvent.VK_LEFT: player[0].moveLeft(level); break;
		  case KeyEvent.VK_RIGHT: player[0].moveRight(level); break;
		  default: break;
	  }
	  repaint();
	}
  
  	private void drawLevel( Graphics g){
  		//hintergrund and umrandung
		g.setColor( Color.WHITE );
	  	g.fillRect( OFFSET, OFFSET, playfieldHeight, playfieldWidth);
	  	g.setColor( Color.BLACK );
	  	g.drawRect( OFFSET, OFFSET, playfieldHeight, playfieldWidth);
	  	g.drawRect( OFFSET-2, OFFSET-2, playfieldHeight+4, playfieldWidth+4);
	  	//grid
	  	for( int i = 1; i < level.getColumnAmount(); i++){
	  		g.drawLine( OFFSET+i*FIELDSIZE, OFFSET, OFFSET+i*FIELDSIZE, OFFSET+playfieldWidth);
	  	}
	  	for( int i = 1; i < level.getRowAmount(); i++){
	  		g.drawLine( OFFSET, OFFSET+i*FIELDSIZE, OFFSET+playfieldHeight, OFFSET+i*FIELDSIZE);
	  	}
	  	//walls
	  	g.setColor( Color.LIGHT_GRAY );
	  	for( int i = 0; i < level.getRowAmount(); i++){
	  		for( int j = 0; j < level.getColumnAmount(); j++){
	  			if( level.getField( i, j).equals( "X")){
	  				g.fillRect( Canvas.OFFSET + Canvas.FIELDSIZE*j + WALLOFFSET,
	  							Canvas.OFFSET + Canvas.FIELDSIZE*i + WALLOFFSET,
	  						  	Canvas.FIELDSIZE - WALLOFFSET*2,
	  						  	Canvas.FIELDSIZE - WALLOFFSET*2);
	  			}
	  		}
	  	}
  	}
  	
  	public void updatePlayer( int playerNo, Player player){
  		this.player[playerNo] = player;
  	}
  	
	/*
	 * wird von repaint aufgerufen
	 */
	public void update(Graphics g){	  
		Dimension d = getSize();
		  
	    // check the offscreen image
		if (mImage == null ||
		mImage.getWidth(null) != d.width ||
		mImage.getHeight(null) != d.height){
		mImage = createImage(d.width, d.height);
		}
		 
		Graphics offG = mImage.getGraphics();
		paint(offG); // draw into the offscreen image
		g.drawImage(mImage, 0, 0, null); // display offscreen image
	}

  
	/*
	 * @see javax.swing.JComponent#paint(java.awt.Graphics)
	 */
	public void paint(Graphics g)
	{
		this.draw( g );
	}
	
	public int getPlayfieldWidth() {
		return playfieldWidth;
	}

	public int getPlayfieldHeight() {
		return playfieldHeight;
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
}
